﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.BO;

public partial class Customer_AllCustomers : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnDelete.Attributes["onclick"] = "if(!preCheckDeleteItems('" + gv.ClientID + "')){return false; event.cancelBubble = true;}";
            DoDataBind();
        }
    }

    void DoDataBind()
    {
        ICustomerManager customerMgr = ManagerFactory.CreateCustomerManager();
        int totalCount = 0;
        List<CustomerInfo> customers = customerMgr.GetPagedCustomers(CallContext, null, pager.CurrentPageIndex, pager.PageSize, out totalCount);

        gv.DataSource = customers;
        gv.DataKeyNames = new string[] { "AID" };
        gv.DataBind();
        BindPagers(customers.Count);
        lblTotal.Text = customers.Count.ToString();
    }

    private void BindPagers(int total)
    {
        pager.RecordCount = total;
        pager2.RecordCount = total;
    }
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        pager2.CurrentPageIndex = pager.CurrentPageIndex;
        DoDataBind();
    }
    protected void pager2_PageChanged(object sender, EventArgs e)
    {
        pager.CurrentPageIndex = pager2.CurrentPageIndex;
        DoDataBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ICustomerManager customerMgr = ManagerFactory.CreateCustomerManager();
        List<int> customerIds = new List<int>();
        foreach (GridViewRow row in gv.Rows)
        {
            CheckBox chk = row.FindControl("chk") as CheckBox;
            if (chk != null && chk.Checked)
            {
                string sid = gv.DataKeys[row.RowIndex].Value.ToString();
                customerIds.Add(int.Parse(sid));
            }
        }

        if (customerIds.Count > 0)
        {
            int count = customerMgr.DeleteCustomer(CallContext, customerIds.ToArray());
            MsgInfo(string.Format("已删除{0}条记录。", count));
            DoDataBind();
        }
        else
        {
            MsgInfo("未选中记录。");
        }
    }
}
